/*
 *Class Name: ListWidget
 *Class Function: 创建item和更新item
*/
#include <QDebug>
#include <QDateTime>
#include <QMouseEvent>
#include <QAction>
#include <QMenu>
#include <QMetaEnum>
#include <QTimer>


#include "listwidget.h"
#include "meatterdata.h"
#include "common.h"

ListWidget::ListWidget(QWidget *parent) :
    QListWidget(parent),meatter(),isDoubleClick(0)
{
    foreach (QString str, MeatterData::ReadFile()) {
        this->addItem(CreateListItem(str));
    }
    time = new QTime();
    timer = new QTimer();
    action = new QAction("Del",this);
    connect(timer,SIGNAL(timeout()),this,SLOT(ItemClick()));
    connect(action,SIGNAL(triggered()),this,SLOT(DelItem()));

}

void ListWidget::mousePressEvent(QMouseEvent *event)
{
    QListWidgetItem * item = itemAt(mapFromGlobal(QCursor::pos()));//这行代码就表明了，QListWidget中所有的item的鼠标事件将全部被改写，不能在使用原有的事件，但是可以在MouseReleaseEvent中进行修补
    if (item != 0) {
        time->start();
        this->setCurrentItem(item);
        ++isDoubleClick;
        event->accept();
    }else {
        cout << "item is Empty" << endl;
        QListWidget::mousePressEvent(event);
    }
}

void ListWidget::mouseReleaseEvent(QMouseEvent *event)
{
    QListWidgetItem * item = itemAt(mapFromGlobal(QCursor::pos()));

    if (item != 0) {
       if (time->elapsed() >500) {//长按事件的处理
            this->setCurrentItem(item);
            ResetFlag();
            MouseLongPress();
        } else if (isDoubleClick == 2) {//双击事件
            this->setCurrentItem(item);
            ResetFlag();
            ItemDoubleClicked_();
            event->accept();
        }else {
           if (!timer->isActive()) {
               timer->start(300);//单击事件直接绑定到信号上并处理到槽
           }
           QListWidget::mouseReleaseEvent(event);
       }
    }
    QListWidget::mouseReleaseEvent(event);
}

//最终的解决方案是一步步的尝试，一步步的输出，一步步的针对。

void ListWidget::ResetFlag()
{
    isDoubleClick =0;
    timer->stop();
}

//单击事件
void ListWidget::ItemClick()
{
    ResetFlag();
    emit itemClicked(this->currentItem());
}

//双击事件
void ListWidget::ItemDoubleClicked_()
{
    emit itemDoubleClicked(this->currentItem());
}

//长按事件
void ListWidget::MouseLongPress()
{
    QMenu* popMenu = new QMenu(this);
    popMenu->addAction(action);
    popMenu->exec(QCursor::pos());
}

//增删改查
void ListWidget::UpdateListContent()
{
    this->clear();
    foreach (QString str, MeatterData::ReadFile()) {
        this->addItem(CreateListItem(str));
    }
}

QListWidgetItem* ListWidget::CreateListItem(QString str)
{
    QListWidgetItem * item = new QListWidgetItem(str);
    item->setSizeHint(QSize(this->rect().width(),35));//设置大小
    return item;
}

void ListWidget::DelItem()
{
    this->takeItem(this->currentRow());
    QStringList strList;
    //修该一下list，使用索引为目标来动态的添加数据。
    for (int i = 0; i < this->count(); ++i) {
        strList.append(this->item(i)->text());
    }

    meatter->WriteFile(strList);
}

